package dshop.action.admin;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.upload.UploadFile;

import appbase.fileUpload.FileService;
import appbase.fileUpload.SavedFile;
import appbase.jfinal.ext.ApiBaseController;
import appbase.third.QiNiuKit;
import dshop.AppConst;
import dshop.interceptor.ApiAuthInterceptor;
import dshop.interceptor.ApiReadBodyParamInterceptor;

@Before({ApiReadBodyParamInterceptor.class, ApiAuthInterceptor.class})
public class CommonAction extends ApiBaseController{

	/**
	 * 七牛上传凭证
	 */
	public void getQiniuToken()
	{
		setAttr("domain", QiNiuKit.DOMAIN);
		setAttr("token", QiNiuKit.getToken());
		
		renderSuccess();
	}
	
	
	@Clear
	public void uploadImage()
	{
		if (isNormalForm()) 
		{
			renderFail();
			return;
		}
		
		UploadFile image = getFile("file");
		
		if(image == null)
		{
			renderFail();
		}
		
		String folder = AppConst.FOLDER_IMAGE_COMPANY_AUTH;
		
		if (folder != null) 
		{
			SavedFile savedFile = FileService.uploadTo(folder, image.getFile());
			String imageUrl = QiNiuKit.upload(savedFile.getAbsolutePath(), FileService.getFileKey(savedFile));
			setAttr("link", imageUrl == null ? savedFile.getDownloadLink() : imageUrl);
		}
		
		renderSuccess();
	}
	
	
	/**
	 * 删除图片
	 */
	/*public void appImageDelete()
	{
		int id = getParaToInt("id", 0);
		int ownerId = getParaToInt("ownerId", 0);
		int type = getParaToInt("type", 0);
		
		AppImage.delete(id, type, ownerId);
		
		renderSuccess();
	}*/
	
	
}
